package org.lb.MarketingSystem.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.task.Task;
import org.lb.MarketingSystem.service.LeaveProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LeaveProcessController {
	@Autowired
	private LeaveProcessService leaveProcessService;
	/**
	 * 页面跳转
	 * @return
	 */
	@RequestMapping(value="/leave")
	public String leave(){
		return "leave";
	}
	/**
	 * 启动流程,发布任务
	 */
	@RequestMapping(value="/leaveProcess")
	@ResponseBody
	public String start(String data){
		String assignee="张三";
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("apply", assignee);
		map.put("msg", "请假流程");
		//发布请假流程
		leaveProcessService.develop();
		//启动请假流程
		leaveProcessService.start(map);
		//发布流程结束
		//执行请假申请
		String[] datas = data.split(",");
		Map<String, Object> newMap = new HashMap<String, Object>();
		newMap.put("approver", datas[4]);
		newMap.put("startTime", datas[1]);
		newMap.put("endTime", datas[2]);
		newMap.put("reason", datas[3]);
		String taskId = leaveProcessService.getTaskIdByAssignee(assignee);
		leaveProcessService.complete(taskId, newMap);
		return taskId;
	}
	/**
	 * 查询任务
	 */
	@RequestMapping(value="/queryTask")
	@ResponseBody
	public List<Map<String, Object>> queryTask(String assignee){
		List<Map<String, Object>> maps = leaveProcessService.queryTask(assignee);
		Map<String, Object> testMap = new HashMap<String, Object>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> map : maps) {
			Task task = (Task) map.get("task");
			testMap.put("taskId", task.getId());
			@SuppressWarnings("unchecked")
			Map<String, Object> variables = (Map<String, Object>) map.get("variables");
			testMap.put("apply", variables.get("apply"));
			testMap.put("startTime", variables.get("startTime"));
			testMap.put("endTime", variables.get("endTime"));
			testMap.put("reason", variables.get("reason"));
			testMap.put("msg", variables.get("msg"));
			list.add(testMap);
		}
		return list;
	}
	
	/**
	 * 完成任务
	 */
	@RequestMapping(value="/complete")
	@ResponseBody
	public Map<String, Object> complete(String taskId,String flag){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("msg", flag);
		//完成任务
		leaveProcessService.complete(taskId, map);
		map.put("success", true);
		return map;
	}
	
	/**
	 * 查询最后结果
	 * @param assignee
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value="/result")
	@ResponseBody
	public Map<String, Object> getResultByAssignee(String assignee){
		Map<String, Object> map = leaveProcessService.getHistoryvarinsByAssignee(assignee);
		return map;
	}
	
}
